#ifndef _HEAD_H
#define _HEAD_H
#include <my_head.h>
#endif

#ifndef _USERMANAGER_
#define _USERMANAGER_
#include"./headfile/alluser/userManager.h"
#endif


/* murmur_hash2 */
static uint32_t hash(const void* key, int len, uint32_t seed) {
    const uint32_t m = 0x5bd1e995;
    const int r = 24;
    uint32_t h = seed ^ len;
    const unsigned char* data = (const unsigned char*)key;

    while (len >= 4) {
        uint32_t k = *(uint32_t*)data;
        k *= m;
        k ^= k >> r;
        k *= m;
        h *= m;
        h ^= k;
        data += 4;
        len -= 4;
    }

    switch (len) {
    case 3: h ^= data[2] << 16;
    case 2: h ^= data[1] << 8;
    case 1: h ^= data[0];
        h *= m;
    };

    h ^= h >> 13;
    h *= m;
    h ^= h >> 15;

    return h;
}
//create只会调用一次
void create_and_init_userM(User_manager * umanager)
{
    umanager = (User_manager *)malloc(sizeof(User_manager));
    CHECK_ERROR(umanager , NULL , "userManager creating occured a fail\n");
    //这几个做头节点
    Buckets * newUsers = (Buckets *)malloc(sizeof(Buckets) * 13);
    CHECK_ERROR(newUsers , NULL , "users in umanager had occured a fail\n");
    memset(newUsers , 0 , sizeof(Buckets) * 13);
    umanager->curr_user_size = 0 ;
    umanager->max_size = 13;
    umanager->users = newUsers;
    srand(time(NULL));
    umanager->seed = rand();
    return;
}

//以hash的方式存储
//可以传函数栈内的地址
bool um_add_user(User_manager * manager , User * user)
{
    printf("userManager.c /64:usermanage is adding\n");
    uint32_t pos = hash(user->token , sizeof(user->token) , manager->seed);
    pos = pos % manager->max_size;

    User * tmp = (User *)malloc(sizeof(User));
    memset(tmp , 0 , sizeof(User));
    strcat(tmp->token , user->token);
    strcat(tmp->account , user->account);
    strcat(tmp->password , user->password);
    strcat(tmp->currPath , user->currPath);
    strcat(tmp->name , user->name);
    tmp->next = NULL;
    if(manager->users[pos] != NULL)
        tmp->next = manager->users[pos]->next;

    manager->users[pos] = tmp;
    return true;
}

//查hash判断是否存在
User * um_judge_user_exist_by_token(const char * token , User_manager *um)
{
    printf("userManager.c/ 86:获取到函数\n");
    return NULL;
    uint32_t pos = hash(token , sizeof(token) , um->seed);
    pos %= um->max_size;
    User * tmp = um->users[pos];
    while(tmp!=NULL)
    {
        if(strcmp(tmp->token , token) == 0)return tmp;
        tmp = tmp->next;
    }
    return NULL;
}

